package edu.pku.sei.transformation.mdtocl;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.ocl.OCL;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.expressions.OCLExpression;
import org.eclipse.ocl.helper.OCLHelper;
/**
 * @deprecated
 * @author FengChao
 *
 */
public class OCLEvaluator {
	public static String evaluate(EObject context, String expression) throws ParserException {
		if (context == null) {
			throw new NullPointerException("context is null");
		} else {
			// create an OCL helper to do our parsing and evaluating
			OCL ocl =  OCL.newInstance(
	                new EcoreEnvironmentFactory(
	                        new DelegatingPackageRegistry(
	                                context.eResource().getResourceSet().getPackageRegistry(),
	                                EPackage.Registry.INSTANCE)));
			OCLHelper helper = ocl.createOCLHelper();
			Object contextClassifier = context.eClass();
        	if (contextClassifier == null) {
        		throw new NullPointerException("contextClassifier is null");
        	}
            helper.setContext(contextClassifier);
			
			OCLExpression<EClassifier> parsed = helper.createQuery(expression);
			return ocl.evaluate(context, parsed).toString();
		}
	}
}
